define(['app','echarts','echartsGL'],function(app,echarts){return {module: function($scope, $http, eventBusService, httpService, params, configService) {
	//获取学校总数
	httpService.post(app.urls.unit_selectCountAtCenter,{}, function (data) {
        if ("0000" === data.code) {
            $scope.form.school = data.data.dw;
        } else {
            alert(data.msg);
        }
    });
	
	
	//学校-办学时间
    httpService.post(app.urls.schoolanalysis_selectPeriod, {}, function (data) {
        if ("0000" === data.code) {
            $scope.period = data.data;
            $scope.period.name = [];
            for (var i = 0; i < $scope.period.length; i++) {
                $scope.period.name.push($scope.period[i].name);
            }
            $scope.echartsInitperiod();
        } else {
            alert(data.msg);
        }
    });
    $scope.echartsInitperiod = function () {
        // 基于准备好的dom，初始化echarts图表
        var myChart = echarts.init(document.getElementById("mobile_analysis_school_constructed_date"));
        var data = $scope.period;
        option = {
            backgroundColor: '#fff',
            legend: {
                orient: 'horizontal',
                bottom: '3%',
                data: $scope.period.name
            },
            series: [{
                type: 'pie',
                selectedMode: 'single',
                radius: ['25%', '48%'],
                center: ['50%', '40%'],
                color: ['#86D560', '#AF89D6', '#59ADF3', '#FF999A', '#FFCC67'],
                label: {
                    normal: {
                        position: 'inner',
                        formatter: '{d}%',
                        textStyle: {
                            color: '#fff',
                            fontWeight: 'bold',
                            fontSize: 12
                        }
                    }
                },
                labelLine: {
                    normal: {
                        show: false
                    }
                },
                data: data
            }, {
                type: 'pie',
                radius: ['48%', '70%'],
                center: ['50%', '40%'],
                itemStyle: {
                    normal: {
                        color: '#F2F2F2'
                    },
                    emphasis: {
                        color: '#ADADAD'
                    }
                },
                label: {
                    normal: {
                        position: 'inner',
                        formatter: '{c}所',
                        textStyle: {
                            color: '#777777',
                            fontWeight: 'bold',
                            fontSize: 12
                        }
                    }
                },
                data: data
            }]
        };
        // 为echarts对象加载数据
        myChart.setOption(option);
    }
	
    //学段
    httpService.post(app.urls.schoolanalysis_selectStage, {}, function (data) {
        if ("0000" === data.code) {
            $scope.stage = data.data;
            $scope.stage.name = [];
            $scope.stage.value = [];
            for (var i = 0; i < $scope.stage.length; i++) {
                $scope.stage.name.push($scope.stage[i].name);
                $scope.stage.value.push($scope.stage[i].value);
            }
            $scope.echartsInitstage();
        } else {
            alert(data.msg);
        }
    });
    $scope.echartsInitstage = function () {
        // 基于准备好的dom，初始化echarts图表
        var myChart = echarts.init(document.getElementById("mobile_analysis_school_fk_dictionary_school_type"));
        var echartData =  $scope.stage.value
        option = {
           tooltip: {
                trigger: 'item',
                formatter: "{b}<br/>{c}所",
            },
            grid: {
                top: '30%',
                left: '5%',
                right: '30%',
                bottom: '3%',
                containLabel: true
            },
            xAxis: [{
                type: 'category',
                data:  $scope.stage.name
            }],
            yAxis: [{
                type: 'value'
            }],

            series: [{
                barMaxWidth: '40%',
                type: 'bar',
                label: {
                    normal: {
                        show: true,
                        position: 'top'
                    }
                },
                itemStyle: {
                    normal: {
                        color: function(params) {
                            var colorList = [
                                '#d5575e', '#9BCA63', '#E87C25', '#60c0dd','#fad860'
                            ];
                            return colorList[params.dataIndex]
                        },
                        label: {
                            show: true,
                            position: 'top',
                            formatter: function(params) {
                                var total = 0;
                                var percent = 0;
                                echartData.forEach(function(value, index) {
                                    total += value;
                                });
                                percent = ((params.value / total) * 100).toFixed(2);
                                return '' + params.name + '\n\n' + params.value + '人，' + '' + percent + '%';
                            },
                        }
                    }
                },
                data: echartData,
            }]
        };
        // 为echarts对象加载数据
        myChart.setOption(option);
    }
    
    
    //学校-行政区分布
    httpService.post(app.urls.schoolanalysis_selectArea, {}, function (data) {
        if ("0000" === data.code) {
            $scope.area = data.data;
            $scope.echartsInitarea();
        } else {
            alert(data.msg);
        }
    });
    $scope.echartsInitarea = function () {
        // 基于准备好的dom，初始化echarts图表
        var myChart = echarts.init(document.getElementById("mobile_analysis_school_fk_area"));
        /**
         此版本通过设置geoindex && seriesIndex: [1] 属性来实现geo和map共存，来达到hover散点和区域显示tooltip的效果

         默认情况下，map series 会自己生成内部专用的 geo 组件。但是也可以用这个 geoIndex 指定一个 geo 组件。这样的话，map 和 其他 series（例如散点图）就可以共享一个 geo 组件了。并且，geo 组件的颜色也可以被这个 map series 控制，从而用 visualMap 来更改。
         当设定了 geoIndex 后，series-map.map 属性，以及 series-map.itemStyle 等样式配置不再起作用，而是采用 geo 中的相应属性。

         http://echarts.baidu.com/option.html#series-map.geoIndex

         并且加了pin气泡图标以示数值大小
         */
        myChart.showLoading();
        //$.getJSON('assets/json/map.json', function (geoJson) {
        httpService.post(app.urls.getMapJson, {}, function (geoJson) {
            echarts.registerMap('danyang', geoJson.data);
            myChart.hideLoading();
            var geoCoordMap = {
                '市区': [119.612501, 32.01592],
                '司徒镇': [119.489644, 32.005806],
                '延陵镇': [119.48863, 31.92866],
                '珥陵镇': [119.586667, 31.882672],
                '导墅镇': [119.676646, 31.837855],
                '皇塘镇': [119.7398, 31.805792],
                '吕城镇': [119.74529, 31.911394],
                '陵口镇': [119.677234, 31.932214],
                '访仙镇': [119.743705, 31.99688],
                '界牌镇': [119.869146, 32.076446],
                '曲阿街道': [119.619358, 32.038828],
                '丹北镇': [119.758418, 32.086358],
                '埤城镇': [119.612501, 32.01592],
                '云阳街道': [119.598015, 31.950065],
                '丹阳经济开发区': [119.614942, 32.004207]
            }
            var data = $scope.area;
            var max = 35, min = 0; // todo
            var maxSize4Pin = 80, minSize4Pin = 30;
            var convertData = function (data) {
                var res = [];
                for (var i = 0; i < data.length; i++) {
                    var geoCoord = geoCoordMap[data[i].name];
                    if (geoCoord) {
                        res.push({
                            name: data[i].name,
                            value: geoCoord.concat(data[i].value)
                        });
                    }
                }
                return res;
            };
            option = {
                tooltip: {
                    trigger: 'item',
                    formatter: function (params) {
                        if (typeof (params.value)[2] == "undefined") {
                            return params.name + ' : ' + params.value;
                        } else {
                            return params.name + ' : ' + params.value[2];
                        }
                    }
                },
                legend: {
                    show: false,
                },
                visualMap: {
                    show: false,
                    min: 0,
                    max: 500,
                    left: 'left',
                    top: 'bottom',
                    text: ['高', '低'], // 文本，默认为数值文本
                    calculable: true,
                    seriesIndex: [1],
                    inRange: {color: ['#fff', '#fff', '#fff']}
                },
                geo: {
                    layoutCenter: ['50%', '50%'],
                    show: true,
                    map: 'danyang',
                    label: {
                        normal: {
                            show: false
                        },
                        emphasis: {
                            show: false,
                        }
                    },
                    roam: false,
                    itemStyle: {
                        normal: {
                            areaColor: '#fff',
                            borderColor: '#86880d',
                        },
                        emphasis: {
                            areaColor: '#86880d',
                        }
                    }
                },
                series: [
                    {
                        name: 'credit_pm2.5',
                        type: 'scatter',
                        zoom: 1.2,
                        coordinateSystem: 'geo',
                        data: convertData(data),
                        symbolSize: function (val) {
                            return val[2] / 10;
                        },
                        label: {
                            normal: {
                                formatter: '{b}',
                                position: 'right',
                                show: true
                            },
                            emphasis: {
                                show: true
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: '#05C3F9'
                            }
                        }
                    },
                    {
                        type: 'map',
                        map: 'danyang',
                        geoIndex: 0,
                        aspectScale: 1, //长宽比
                        showLegendSymbol: false, // 存在legend时显示
                        label: {
                            normal: {
                                show: false
                            },
                            emphasis: {
                                show: false,
                                textStyle: {
                                    color: '#fff'
                                }
                            }
                        },
                        roam: true,
                        itemStyle: {
                            normal: {
                                areaColor: '#031525',
                                borderColor: '#3B5077',
                            },
                            emphasis: {
                                areaColor: '#2B91B7'
                            }
                        },
                        animation: false,
                        data: data
                    },
                    {
                        name: '点',
                        type: 'scatter',
                        coordinateSystem: 'geo',
                        symbol: 'pin',
                        symbolSize: function (val) {
                            var a = (maxSize4Pin - minSize4Pin) / (max - min);
                            var b = minSize4Pin - a * min;
                            b = maxSize4Pin - a * max;
                            return a * val[2] + b;
                        },
                        label: {
                            normal: {
                                show: true,
                                textStyle: {
                                    color: '#fff',
                                    fontSize: 9,
                                },
                                formatter(value) {
                                    return value.data.value[2] + '所'
                                }
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: '#0c8642', //标志颜色
                            }
                        },
                        zlevel: 6,
                        data: convertData(data),
                    },
                    {
                        name: 'Top 5',
                        type: 'effectScatter',
                        coordinateSystem: 'geo',
                        data: convertData(data.sort(function (a, b) {
                            return b.value - a.value;
                        }).slice(0, 5)),
                        symbolSize: function (val) {
                            return val[2] / 10;
                        },
                        showEffectOn: 'render',
                        rippleEffect: {
                            brushType: 'stroke'
                        },
                        hoverAnimation: true,
                        label: {
                            normal: {
                                formatter: '{b}',
                                position: 'right',
                                show: true
                            }
                        },
                        itemStyle: {
                            normal: {
                                color: '#05C3F9',
                                shadowBlur: 10,
                                shadowColor: '#05C3F9'
                            }
                        },
                        zlevel: 1
                    },

                ]
            };
            myChart.setOption(option);
        });
    }

}}});